package cn.com.nxt.txb;

import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

import cn.com.nxt.txb.fragment.MineFragment;
import cn.com.nxt.txb.httpdelegate.UserDelegate;
import cn.com.nxt.txb.model.User;
import cn.com.nxt.txb.util.AppUtil;
import cn.com.nxt.txb.util.AuthPreferences;
import cn.com.nxt.txb.widget.ClearEditText;

@ContentView(R.layout.activity_modify_password)
public class ModifyPasswordActivity extends BaseActivity {
    @ViewInject(R.id.toolbar)
    private Toolbar toolbar;
    @ViewInject(R.id.et_old_password)
    private ClearEditText mOldPwdEt;
    @ViewInject(R.id.et_new_password)
    private ClearEditText mNewPwdEt;
    private byte nameFlag = 0x005;
    private UserDelegate delegate;
    private String mOldPassword,mNewPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);//注解绑定
        setSupportActionBar(toolbar);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        //toolbar.setNavigationIcon(R.drawable.back_main);
        delegate = new UserDelegate(ModifyPasswordActivity.this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.modify_password, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.save_new:
                save();
                break;
        }
        return true;
    }

    private void save() {
        mOldPassword = mOldPwdEt.getText().toString().trim();
        if (TextUtils.isEmpty(mOldPassword)) {
            showToast("当前密码不能为空！");
            return;
        }
        mNewPassword = mNewPwdEt.getText().toString().trim();
        if (TextUtils.isEmpty(mNewPassword)) {
            showToast("新密码不能为空！");
            return;
        }
        Map<String,String> map=new HashMap<>();
        map.put("phone", AuthPreferences.getUserAccount());
        map.put("oldpassword",mOldPassword);
        map.put("newpassword",mNewPassword);
        Gson gson = new Gson();
        showDialog("正在修改");
        Log.v("--password--",gson.toJson(map));
        delegate.modifyPassword(gson.toJson(map), AppUtil.mUser.getToken(), nameFlag);
    }

    @Override
    public void success(String result, byte flag) {
        super.success(result, flag);
        disDialog();
        AppUtil.mUser.setUsername(mNewPassword);
        AppUtil.saveUser(ModifyPasswordActivity.this,AppUtil.mUser);
        showToast("修改成功");
        //setResult(UserInfoActivity.RESULT_NAME, new Intent());
        finish();
    }

    @Override
    public void failure(String result, byte flag, ResponseStatus status) {
        super.failure(result, flag, status);
        disDialog();
        showToast(result);
    }

    @Override
    public void finished(int flag) {
        disDialog();
    }

    @Override
    public void cancelled(int flag) {
        disDialog();
    }
}
